using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ClothingItem : BaseItem
	{
		public enum State
		{
			Fold = 0,
			Unfold = 1
		}

		public State state;

		public Transform body;

		public int topsIconId;

		private bool isInitBody;

		private SpriteAssets spriteAssets;

		private Transform icon;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			body = model.Find("Body");
			topsIconId = data.topsIconId;
			icon = model.Find("Icon");
			spriteAssets = base.transform.GetComponent<SpriteAssets>();
			if (base.itemData.type == 4)
			{
				SetFoldSprite(base.itemData.topsId);
			}
			else
			{
				SetFoldSprite(base.itemData.pantsId);
			}
		}

		public void SetState(State val)
		{
			if (state == val)
			{
				return;
			}
			state = val;
			if (state == State.Fold)
			{
				icon.gameObject.SetActive(true);
				body.gameObject.SetActive(false);
				if (base.itemData.type == 4)
				{
					coll.offset = new Vector2(0f, 0.21f);
					coll.size = new Vector2(0.57f, 0.426f);
				}
				else
				{
					coll.offset = new Vector2(0f, 0.19f);
					coll.size = new Vector2(0.58f, 0.38f);
				}
				SetPlatformColliderEnable(true);
				return;
			}
			icon.gameObject.SetActive(false);
			InitBody();
			body.gameObject.SetActive(true);
			if (base.itemData.type == 4)
			{
				coll.offset = new Vector2(0f, 0.38f);
				coll.size = new Vector2(0.5f, 0.6f);
			}
			else
			{
				coll.offset = new Vector2(0f, 0.24f);
				coll.size = new Vector2(0.5f, 0.6f);
			}
			SetPlatformColliderEnable(false);
		}

		public void InitBody()
		{
			if (isInitBody)
			{
				return;
			}
			isInitBody = true;
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			string[] array = null;
			if (!string.IsNullOrEmpty(base.itemData.scriptParams))
			{
				array = base.itemData.scriptParams.Split(',');
				if (array.Length != 5)
				{
					Trace.LogError(base.itemData.id + " 衣服配置错误！");
					array = null;
				}
			}
			if (base.itemData.type == 4)
			{
				body.Find("LeftHand/UpperArm").localPosition = new Vector3(0f, 0f, 2E-05f);
				body.Find("LeftHand/UpperArm/LowerArm").SetLocalZ(-1E-05f);
				body.Find("RightHand/UpperArm").localPosition = new Vector3(0f, 0f, 2E-05f);
				body.Find("RightHand/UpperArm/LowerArm").SetLocalZ(-1E-05f);
				body.Find("Tops/Icon").SetLocalZ(-1E-05f);
				body.Find("Tops/Back").SetLocalZ(1E-05f);
				SetIcon(topsIconId);
				ChangeSprite(body.Find("Tops/Sprite"), transform.Find("Model/Body/Tops/" + base.itemData.topsId));
				ChangeSprite(body.Find("Tops/Back/Sprite"), transform.Find("Model/Body/Tops/" + base.itemData.topsId + "Back"));
				ChangeSprite(body.Find("LeftHand/UpperArm/Clothing/Sprite"), transform.Find("Model/Body/LeftHand/UpperArm/Clothing/" + base.itemData.topsId));
				if (array == null || array[2].Equals("1"))
				{
					ChangeSprite(body.Find("LeftHand/UpperArm/LowerArm/Clothing/Sprite"), transform.Find("Model/Body/LeftHand/UpperArm/LowerArm/Clothing/" + base.itemData.topsId));
				}
				ChangeSprite(body.Find("RightHand/UpperArm/Clothing/Sprite"), transform.Find("Model/Body/RightHand/UpperArm/Clothing/" + base.itemData.topsId));
				if (array == null || array[4].Equals("1"))
				{
					ChangeSprite(body.Find("RightHand/UpperArm/LowerArm/Clothing/Sprite"), transform.Find("Model/Body/RightHand/UpperArm/LowerArm/Clothing/" + base.itemData.topsId));
				}
			}
			else if (base.itemData.type == 5)
			{
				body.Find("LeftLeg/UpperLeg").localPosition = new Vector3(0f, 0f, 1E-05f);
				body.Find("LeftLeg/UpperLeg/LowerLeg").SetLocalZ(1E-05f);
				body.Find("RightLeg/UpperLeg").localPosition = new Vector3(0f, 0f, 1E-05f);
				body.Find("RightLeg/UpperLeg/LowerLeg").SetLocalZ(1E-05f);
				ChangeSprite(body.Find("Pants/Sprite"), transform.Find("Model/Body/Pants/" + base.itemData.pantsId));
				ChangeSprite(body.Find("LeftLeg/UpperLeg/Clothing/Sprite"), transform.Find("Model/Body/LeftLeg/UpperLeg/Clothing/" + base.itemData.pantsId));
				ChangeSprite(body.Find("LeftLeg/UpperLeg/LowerLeg/Clothing/Sprite"), transform.Find("Model/Body/LeftLeg/UpperLeg/LowerLeg/Clothing/" + base.itemData.pantsId));
				ChangeSprite(body.Find("RightLeg/UpperLeg/Clothing/Sprite"), transform.Find("Model/Body/RightLeg/UpperLeg/Clothing/" + base.itemData.pantsId));
				ChangeSprite(body.Find("RightLeg/UpperLeg/LowerLeg/Clothing/Sprite"), transform.Find("Model/Body/RightLeg/UpperLeg/LowerLeg/Clothing/" + base.itemData.pantsId));
			}
		}

		public override bool CanAttatchItem()
		{
			if (childItems.Count > 0)
			{
				return false;
			}
			return base.CanAttatchItem();
		}

		public void SetIcon(int id)
		{
			if (topsIconId != id)
			{
				topsIconId = id;
				if (topsIconId < 1)
				{
					body.Find("Tops/Icon/Sprite").GetComponent<SpriteRenderer>().sprite = null;
				}
				else
				{
					body.Find("Tops/Icon/Sprite").GetComponent<SpriteRenderer>().sprite = Singleton<AssetBundleManager>.Instance.GetTopsIcon(topsIconId);
				}
			}
		}

		public void SetFoldSprite(int id)
		{
			SpriteRenderer component = icon.GetComponent<SpriteRenderer>();
			component.sprite = spriteAssets.GetSprite(id.ToString());
			icon.localPosition = new Vector3(0f, component.bounds.size.y / 2f, 0f);
		}

		private void ChangeSprite(Transform targetTrans, Transform spriteTrans)
		{
			if (spriteTrans == null)
			{
				targetTrans.GetComponent<SpriteRenderer>().sprite = null;
				return;
			}
			targetTrans.GetComponent<SpriteRenderer>().sprite = spriteTrans.GetComponent<SpriteRenderer>().sprite;
			targetTrans.localPosition = spriteTrans.localPosition;
		}

		public override void ResetState()
		{
			base.ResetState();
			CheckState();
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (childItems.Count < 1)
			{
				SetState(State.Unfold);
			}
			base.TouchDown(touchEventData);
		}

		public void CheckState()
		{
			if (attachType == AttachType.Wear)
			{
				base.gameObject.SetActive(false);
				return;
			}
			base.gameObject.SetActive(true);
			if (parentItem != null && (parentItem.itemData.type == 3 || parentItem.itemData.scriptType == 17))
			{
				SetState(State.Unfold);
			}
			else
			{
				SetState(State.Fold);
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			CheckState();
		}

		protected override void OnTriggerEnter2D(Collider2D collision)
		{
			if (isDrag && state == State.Unfold && collision.gameObject.layer == 12)
			{
				ItemComponentTrigger component = collision.gameObject.GetComponent<ItemComponentTrigger>();
				if (component != null && (component.type == ItemComponentTrigger.Type.LeftHand || component.type == ItemComponentTrigger.Type.RightHand))
				{
					return;
				}
			}
			base.OnTriggerEnter2D(collision);
		}

		protected override void CheckColls()
		{
			if (childItems.Count < 1)
			{
				for (int i = 0; i < itemCompColls.Count; i++)
				{
					ItemComponentTrigger.Type type = itemCompColls[i].trigger.type;
					if (base.itemData.type == 4)
					{
						if (type == ItemComponentTrigger.Type.Body && itemCompColls[i].trigger.owner.itemData.scriptType == 1 && ((CharacterItem)itemCompColls[i].trigger.owner).ChangeTops(this))
						{
							CheckState();
							return;
						}
					}
					else if ((type == ItemComponentTrigger.Type.LeftLeg || type == ItemComponentTrigger.Type.RightLeg) && itemCompColls[i].trigger.owner.itemData.scriptType == 1 && ((CharacterItem)itemCompColls[i].trigger.owner).ChangePants(base.itemData.pantsId))
					{
						GlobalEventSystem.Fire(new BaseEvent("game.remove_item", archiveData.index));
						return;
					}
				}
			}
			base.CheckColls();
		}

		public override bool CanPlaceItem(BaseItem item)
		{
			if (state == State.Unfold || childItems.Count > 0)
			{
				return false;
			}
			return base.CanPlaceItem(item);
		}

		protected override bool CanPlaceOnPlatform(BaseItem item)
		{
			if (childItems.Count > 0 && item.itemData.type == 3)
			{
				return false;
			}
			return base.CanPlaceOnPlatform(item);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["17"] = topsIconId;
		}
	}
}
